package com.app.mingluxing.location;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.SafeAreaResult;
import com.app.mingluxing.utils.LogUtil;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapLoadedCallback;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.DotOptions;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;

/**
 * 安全区域详情
 *
 * @author Aaron
 */
public class SafeAreaDetailActivity extends BaseActivity {
    public final static String TAG = "SafeAreaDetailActivity";
    private TextView tv_title;

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LatLng mLatLng = null;
    private int mRadius;
    private String mSafeAreaName;
    private SafeAreaResult.AreaList mSafeAreaList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.safe_area_detail);
        mSafeAreaList = (SafeAreaResult.AreaList) getIntent().getSerializableExtra("safeArea");

        // 加载控件
        initView();
        initValue();

        setListener();

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:

                finish();

                break;

            default:
                break;
        }

    }

    /**
     * 初始化变量
     */
    private void initValue() {
        if (mSafeAreaList != null) {
            mLatLng = new LatLng(mSafeAreaList.lat, mSafeAreaList.lng);

            mRadius = mSafeAreaList.radius;
        }
    }

    private void setListener() {

        mBaiduMap.setOnMapLoadedCallback(new OnMapLoadedCallback() {

            @Override
            public void onMapLoaded() {
                LogUtil.e(TAG, "onMapLoaded");
                createPoint();
            }
        });

    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.safe_area_detail));

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        iv_left.setOnClickListener(this);

        // 地图初始化
        // 获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();

    }

    /**
     * 创建覆盖物
     */
    private void createPoint() {

        mBaiduMap.clear();
        // 添加圆
        if (mLatLng != null) {
            OverlayOptions ooCircle = new CircleOptions().fillColor(0x4d35a9e2)
                    .center(mLatLng).stroke(new Stroke(5, 0x8035a9e2))
                    .radius(mRadius);

            mBaiduMap.addOverlay(ooCircle);

            // 添加点
            OverlayOptions ooDot = new DotOptions().center(mLatLng).radius(6)
                    .color(0xff35a9e2);
            mBaiduMap.addOverlay(ooDot);
            // /**
            // * 各级比例尺分母值数组 地图缩放级别 3~19
            // */
            // private static final int[] SCALES = { 20, 50, 100, 200, 500,
            // 1000,
            // 2000, 5000, 10000, 20000, 25000, 50000, 100000, 200000,
            // 500000, 1000000, 2000000 };
            float zoom = 19;
            // 定位到该点
            if (mRadius > 20 && mRadius <= 50) {
                zoom = 18;
            } else if (mRadius > 50 && mRadius <= 100) {
                zoom = 17;
            } else if (mRadius > 100 && mRadius <= 200) {
                zoom = 16;
            } else if (mRadius > 200 && mRadius <= 500) {
                zoom = 15;
            } else if (mRadius > 500 && mRadius <= 1000) {
                zoom = 14;
            }
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(mLatLng,
                    zoom);
            mBaiduMap.setMapStatus(u);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMapView != null) {
            mMapView.onDestroy();
            mMapView = null;
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        if (mMapView != null)
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        // 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        if (mMapView != null)
        mMapView.onPause();

    }


}
